package com.platform.service;

import com.platform.dao.ApiSysUserMapper;
import com.platform.entity.SysUserVo;
import com.platform.utils.ApiRRException;
import com.platform.utils.Constant;
import com.platform.utils.RRException;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 *
 * @author 李鹏军
 * @email 939961241@qq.com
 * @date 2016年12月18日 上午9:46:09
 */
@Service
public class ApiSysUserServiceImpl {

    @Autowired
    private ApiSysUserMapper apiSysUserMapper;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleService sysRoleService;

    public List<String> queryAllPerms(Long userId) {
        return apiSysUserMapper.queryAllPerms(userId);
    }

    public List<Long> queryAllMenuId(Long userId) {
        return apiSysUserMapper.queryAllMenuId(userId);
    }

    public SysUserVo queryByUserName(String username) {
        return apiSysUserMapper.queryByUserName(username);
    }

    public List<SysUserVo> queryList(Map<String, Object> map) {
        return apiSysUserMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiSysUserMapper.queryTotal(map);
    }

    /**
     * 根据用户id查询 用户信息
     * @param userId
     * @return
     */
    public SysUserVo queryObject(Long userId) {
        return apiSysUserMapper.queryObject(userId);
    }
    /**
     * 根据 客户id查询 用户信息
     * @param customerId
     * @return
     */
    public SysUserVo queryObjectByCstId(Long customerId) {
        return apiSysUserMapper.queryObjectByCstId(customerId);
    }

    @Transactional
    public void save(SysUserVo user) {

        Map<String, Object> param = new HashedMap();
        //判断 微信用户 是否已经绑定
        param.put("customerId",user.getCustomerId());
        List<SysUserVo> sysUserVos = apiSysUserMapper.queryBy(param);
        if(sysUserVos!=null && sysUserVos.size()>0) {
            apiSysUserMapper.updateByCustomerId(user);
            return ;
        }
        param.clear();
        //判断 用户名 是否已经存在
        param.put("username",user.getUsername());
        List<SysUserVo> sysUserVos1 = apiSysUserMapper.queryBy(param);
        if(sysUserVos1!=null && sysUserVos1.size()>0) {
            throw new ApiRRException("已存在此用户名，请更改");
        }

        user.setCreateTime(new Date());
        //sha256加密
        user.setPassword(new Sha256Hash(Constant.DEFAULT_PASS_WORD).toHex());
        apiSysUserMapper.save(user);

        //检查角色是否越权
//        checkRole(user);

        //保存用户与角色关系
//        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Transactional
    public void update(SysUserVo user) {
        if (StringUtils.isBlank(user.getPassword())) {
            user.setPassword(new Sha256Hash(Constant.DEFAULT_PASS_WORD).toHex());
        } else {
            user.setPassword(new Sha256Hash(user.getPassword()).toHex());
        }
        apiSysUserMapper.updateByCustomerId(user);

        //检查角色是否越权
//        checkRole(user);

        //保存用户与角色关系
//        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Transactional
    public void deleteBatch(Long[] userId) {
        apiSysUserMapper.deleteBatch(userId);
    }

    public int updatePassword(Long userId, String password, String newPassword) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("password", password);
        map.put("newPassword", newPassword);
        return apiSysUserMapper.updatePassword(map);
    }

    /**
     * 检查角色是否越权
     */
    private void checkRole(SysUserVo user) {
        //如果不是超级管理员，则需要判断用户的角色是否自己创建
        if (user.getCreateUserId() == Constant.SUPER_ADMIN) {
            return;
        }

        //查询用户创建的角色列表
        List<Long> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUserId());

        //判断是否越权
        if (!roleIdList.containsAll(user.getRoleIdList())) {
            throw new RRException("新增用户所选角色，不是本人创建");
        }
    }

//    public Page<UserWindowDto> findPage(UserWindowDto userWindowDto, int pageNum) {
//        PageHelper.startPage(pageNum, Constant.pageSize);
//        apiSysUserMapper.queryListByBean(userWindowDto);
//        return PageHelper.endPage();
//    }
}
